#!/bin/bash
#
# Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Rotation - Save/Load/List/MI"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
SESSION_NAME="stream"

NUM_TESTS=22

source $TESTDIR/utils/utils.sh
source $CURDIR/rotate_utils.sh

# Overwrite the lttng_bin to get mi output
LTTNG_BIN="lttng --mi xml"

XPATH_CMD_OUTPUT="//lttng:command/lttng:output"
XPATH_SESSION="$XPATH_CMD_OUTPUT/lttng:sessions/lttng:session"
XPATH_ENABLE_ROTATE_TIMER="$XPATH_CMD_OUTPUT/lttng:rotation_schedule_results/lttng:rotation_schedule_result/lttng:rotation_schedule/lttng:periodic/lttng:time_us"
XPATH_ENABLE_ROTATE_SIZE="$XPATH_CMD_OUTPUT/lttng:rotation_schedule_results/lttng:rotation_schedule_result/lttng:rotation_schedule/lttng:size_threshold/lttng:bytes"

XPATH_LIST_ROTATE_TIMER="$XPATH_SESSION/lttng:rotation_schedules/lttng:periodic/lttng:time_us"
XPATH_LIST_ROTATE_SIZE="$XPATH_SESSION/lttng:rotation_schedules/lttng:size_threshold/lttng:bytes"

function test_save_load_mi ()
{
	local tmp_xml_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_xml_output.XXXXXX")
	local tmp_save_output=$(mktemp -d -t "tmp.${FUNCNAME[0]}_save_output.XXXXXX")
	local trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")

	diag "Test save/load/list/MI with rotation"
	create_lttng_session_ok $SESSION_NAME $trace_path
	enable_ust_lttng_event_ok $SESSION_NAME -a

	# Enable a rotation timer and check the MI output of the command

	OUTPUT_DEST=${tmp_xml_output}
	lttng_enable_rotation_timer_ok $SESSION_NAME 500ms
	$MI_VALIDATE ${tmp_xml_output}
	ok $? "Valid lttng enable-rotation timer XML"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_ENABLE_ROTATE_TIMER})
	test $value = 500000
	ok $? "Found the right rotation timer value in XML"

	# Enable a rotation size and check the MI output of the command
	lttng_enable_rotation_size_ok $SESSION_NAME 500k
	$MI_VALIDATE ${tmp_xml_output}
	ok $? "Valid lttng enable-rotation size XML"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_ENABLE_ROTATE_SIZE})
	test $value = 512000
	ok $? "Found the right rotation size value in XML"

	# Check the output of lttng list with the 2 rotation parameters
	list_lttng_ok $SESSION_NAME
	$MI_VALIDATE ${tmp_xml_output}
	ok $? "Valid lttng list XML"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_LIST_ROTATE_TIMER})
	test $value = 500000
	ok $? "Found the right rotation timer value in list XML"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_LIST_ROTATE_SIZE})
	test $value = 512000
	ok $? "Found the right rotation size value in list XML"

	# save, destroy and reload the same session
	OUTPUT_DEST=/dev/null
	lttng_save $SESSION_NAME "-o $tmp_save_output/$SESSION_NAME"
	destroy_lttng_session_ok $SESSION_NAME
	lttng_load_ok "-i $tmp_save_output/$SESSION_NAME"

	OUTPUT_DEST=${tmp_xml_output}
	# Check the output of lttng list with the 2 rotation parameters after the load
	list_lttng_ok $SESSION_NAME
	$MI_VALIDATE ${tmp_xml_output}
	ok $? "Valid lttng list XML after load"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_LIST_ROTATE_TIMER})
	test $value = 500000
	ok $? "Found the right rotation timer value in list XML after load"

	value=$($XML_EXTRACT ${tmp_xml_output} ${XPATH_LIST_ROTATE_SIZE})
	test $value = 512000
	ok $? "Found the right rotation size value in list XML after load"

	OUTPUT_DEST=/dev/null
	destroy_lttng_session_ok $SESSION_NAME

	rm -rf "$trace_path"
	rm "$tmp_xml_output"
	rm -rf "$tmp_save_output"
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

start_lttng_sessiond

tests=( test_save_load_mi )

for fct_test in ${tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test}
done

stop_lttng_sessiond
